package com.traffic.between.service;

import java.security.MessageDigest;

import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.log4j.Logger;

import com.traffic.between.config.BetweenProperties;
import com.traffic.between.util.Xml2JsonUtil;

import net.sf.json.JSONObject;

public class ShenzltServiceImpl {
	private static Logger logger = Logger.getLogger(ShenzltServiceImpl.class);
	public static ShenzltServiceImpl service = new ShenzltServiceImpl();

	private static String HOST_ORDER_PATH = BetweenProperties.SZLT_ORDER_URL;
	private static String HOST_ORDERSTATUS_PATH = BetweenProperties.SZLT_ORDERSTATUS_URL;
	private static String SZLT_APPKEY = BetweenProperties.SZLT_APPKEY;
	private static String SZLT_SECURITYKEY = BetweenProperties.SZLT_SECURITYKEY;


	public String order(String productId, String phoneNo, String cstmOrderNo) {
		String res="";
		try {  
			  
  
            Service service = new Service();  
            Call call = (Call) service.createCall();  
            call.setTargetEndpointAddress(HOST_ORDER_PATH);  
            // WSDL里面描述的接口名称(要调用的方法)  
            call.setOperationName("productOpen");  
            // 接口方法的参数名, 参数类型,参数模式  IN(输入), OUT(输出) or INOUT(输入输出)  
            call.addParameter("pkey", XMLType.XSD_STRING, ParameterMode.IN);  
            call.addParameter("seqId", XMLType.XSD_STRING, ParameterMode.IN);  
            call.addParameter("loginName", XMLType.XSD_STRING, ParameterMode.IN);  
            call.addParameter("phoneNo", XMLType.XSD_STRING, ParameterMode.IN); 
            call.addParameter("productCode", XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter("code", XMLType.XSD_STRING, ParameterMode.IN);
            
            //按参数名降序排列顺序为： seqId, productCode, pkey, phoneNo , loginName
            String str =cstmOrderNo+productId+SZLT_APPKEY+phoneNo+"szyp"+ SZLT_SECURITYKEY;
    		String sign = getMD5(str);
            // 设置被调用方法的返回值类型  
            call.setReturnType(XMLType.XSD_STRING); 
            
            //设置方法中参数的值  
            Object[] paramValues = new Object[] {SZLT_APPKEY,cstmOrderNo,"szyp",phoneNo,productId,sign};  
            // 给方法传递参数，并且调用方法  
            res = (String) call.invoke(paramValues);    
      
            String body = Xml2JsonUtil.xml2JSON(res);
			JSONObject resultJson = JSONObject.fromObject(body);
			JSONObject json = (JSONObject) resultJson.get("AgentRsp");
			String json1 = (String) json.get("ResultCode");
			logger.info("between- 深圳联通流量订购请求时JSON:  " + json1);
				if ("0".equals(json1)) {
					res = "{\"resultCode\": \"00\",\"resultDesc\":\"订购中\"}";
				} else {
					res = "{\"resultCode\": \"01\",\"resultDesc\":\"充值失败\"}";
				}
        } catch (Exception e) {  
            e.printStackTrace(); 
            logger.error("between- 深圳联通流量订购请求时的响应异常: " + e);
        }
		return res;
	}

	public String orderStatus(String cstmOrderNo) {

		String res="";
		try {  
			  
            //String endpoint = "http://211.95.193.67/axis/services/IMBackwardProductService?wsdl";  
  
            Service service = new Service();  
            Call call = (Call) service.createCall();  
            call.setTargetEndpointAddress(HOST_ORDERSTATUS_PATH);  
            // WSDL里面描述的接口名称(要调用的方法)  
            call.setOperationName("orderResult");  
            // 接口方法的参数名, 参数类型,参数模式  IN(输入), OUT(输出) or INOUT(输入输出)  
            call.addParameter("pkey", XMLType.XSD_STRING, ParameterMode.IN);  
            call.addParameter("seqId", XMLType.XSD_STRING, ParameterMode.IN);  
            call.addParameter("code", XMLType.XSD_STRING, ParameterMode.IN);
            
            //按参数名降序排列顺序为： seqId, productCode, pkey, phoneNo , loginName
            String str =cstmOrderNo+SZLT_APPKEY+ SZLT_SECURITYKEY;
    		String sign = getMD5(str);
            // 设置被调用方法的返回值类型  
            call.setReturnType(XMLType.XSD_STRING); 
            
            //设置方法中参数的值  
            Object[] paramValues = new Object[] {SZLT_APPKEY,cstmOrderNo,sign};  
            // 给方法传递参数，并且调用方法  
            res = (String) call.invoke(paramValues);    
      
            String body = Xml2JsonUtil.xml2JSON(res);
			JSONObject resultJson = JSONObject.fromObject(body);
			JSONObject json = (JSONObject) resultJson.get("AgentRsp");
			String json1 = (String) json.get("ResultCode");
			logger.info("between- 深圳联通流量订购请求时JSON:  " + json1);
				if ("0".equals(json1)) {
					res = "{\"resultCode\": \"00\",\"resultDesc\":\"订购中\"}";
				} else {
					res = "{\"resultCode\": \"01\",\"resultDesc\":\"充值失败\"}";
				}
        } catch (Exception e) {  
            e.printStackTrace(); 
            logger.error("between- 深圳联通流量订购请求时的响应异常: " + e);
        }
		return res;

	}
	
	public static String getMD5(String message) {
		System.out.println("getMD5 " + message);
		MessageDigest messageDigest = null;
		StringBuffer md5StrBuff = new StringBuffer();
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(message.getBytes("UTF-8"));

			byte[] byteArray = messageDigest.digest();
			for (int i = 0; i < byteArray.length; i++) {
				if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
					md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
				else
					md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
			}
		} catch (Exception e) {
			throw new RuntimeException();
		}
		System.out.println("getMD5====>" + md5StrBuff.toString().toUpperCase());
		return md5StrBuff.toString().toUpperCase();// 字母大写
	}

}
